<?php

namespace App\Models;

use App\Services\AlipayService;
use App\Services\GoodsService;
use App\Services\OrderService;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class OrderRate extends Model
{
    use HasFactory;

    protected $table = 'sp_order_rate';
    protected $primaryKey = 'id';
    public $timestamps = false;

    //添加订单费率
    protected function addOrderRate($order_id,$goods_id,$order_from,$member_id,$is_skip_risk,$month_total,$financing_type,$huabei_stages,$huabei_fee_bear,$pay_type){
        if(!empty($goods_id) && $order_from){
            $category_id = Goods::where('id',$goods_id)->value('category_id');
            $source_id = Source::where('source_code',trim($order_from))->value('id');
            if(!empty($category_id) && !empty($source_id)){
                $goods_rate = GoodsRate::where([['member_id',$member_id],['category_id',$category_id],['source_id',$source_id],['is_delete',GoodsRate::GOODS_RATE_NOT_DELETE]])->select('goods_rate','smartcontract','realname','risk','platform','withhold','file','financing')->first();
                $goods_rate = $goods_rate ? $goods_rate->toArray() : [];
                if(!empty($goods_rate)){
                    if($financing_type == Order::FINANCING_TYPE_RONG){
                        $insure_rate = InsureRate::getInsureRate($goods_id,$month_total);
                        $data['financing_rate'] = $goods_rate['financing'];
                    }else{
                        $insure_rate = 0;
                        $data['financing_rate'] = 0;
                    }
                    $data['order_id'] = $order_id;
                    if($is_skip_risk == Order::RISK_SKIP_YES){
                        $data['risk_amount'] = 0;
                        $data['fixed_amount'] = $goods_rate['smartcontract'] + $goods_rate['realname'];
                    }else{
                        $data['risk_amount'] = $goods_rate['risk'];
                        $data['fixed_amount'] = $goods_rate['smartcontract'] + $goods_rate['realname'] + $goods_rate['risk'];
                    }

                    $data['huabei_rate'] = 0;
                    if($huabei_fee_bear == GoodsService::GOODS_HUABEI_FEE_MERCHANT){//商户承担花呗手续费
                        $huabei_rate = GoodsHuabei::HUABEI_SHOP_FEE_BEAR_LIST[$huabei_stages];
                        $data['huabei_rate'] = $huabei_rate;
                    }
                    $data['withhold_rate'] = $goods_rate['withhold'];
                    if($pay_type == Order::ORDER_PAY_TYPE_ANTCHAIN && $source_id == 7){//店铺扫码 智能合同订单费率+0.5%
                        $data['withhold_rate'] = $goods_rate['withhold'] + 0.5;
                    }
                    if($goods_id == 1141){
                        $data['withhold_rate'] = 44.95;
                    }elseif ($goods_id == 820){
                        $data['withhold_rate'] = 41.95;
                    }elseif ($goods_id == 1024){
                        $data['withhold_rate'] = 16.95;
                    }
                    $data['shop_rate'] = $goods_rate['platform'] + $data['withhold_rate'] + $goods_rate['file'] + $data['financing_rate'] + $insure_rate + $data['huabei_rate'];
                    $data['smartcontract_amount'] = $goods_rate['smartcontract'];
                    $data['realname_amount'] = $goods_rate['realname'];
                    $data['platform_rate'] = $goods_rate['platform'];
                    $data['file_rate'] = $goods_rate['file'];
//                    $data['is_preposition'] = $goods_rate['is_preposition'];
                    $data['insure_rate'] = $insure_rate;
                    $data['create_time'] = date('Y-m-d H:i:s');
                    $data['update_time'] = date('Y-m-d H:i:s');
                    $this->insert($data);
                }
            }
        }
    }


   //编辑订单费率
    protected function updateOrderRate($order_id,$goods_id,$order_from,$pay_type,$is_skip_risk,$financing_type,$member_id){
        $order_res= $this->where('order_id',$order_id)->select('shop_rate','smartcontract_amount','realname_amount','risk_amount','platform_rate','withhold_rate','file_rate','financing_rate','insure_rate','huabei_rate')->first();

        if(!empty($goods_id) && $order_from&&$order_res){
            $category_id = Goods::where('id',$goods_id)->value('category_id');
            $source_id = Source::where('source_code',trim($order_from))->value('id');
            if(!empty($category_id) && !empty($source_id)){
                $goods_rate = GoodsRate::where([['member_id',$member_id],['category_id',$category_id],['source_id',$source_id],['is_delete',GoodsRate::GOODS_RATE_NOT_DELETE]])->select('goods_rate','smartcontract','realname','risk','platform','withhold','file','financing')->first();
                $goods_rate = $goods_rate ? $goods_rate->toArray() : [];
                if(!empty($goods_rate)){
                    if($financing_type == Order::FINANCING_TYPE_RONG){
                        $insure_rate = InsureRate::getInsureRate($goods_id,$month_total);
                        $data['financing_rate'] = $goods_rate['financing'];
                    }else{
                        $insure_rate = 0;
                        $data['financing_rate'] = 0;
                    }
                    $data['order_id'] = $order_id;
                    if($is_skip_risk == Order::RISK_SKIP_YES){
                        $data['risk_amount'] = 0;
                        $data['fixed_amount'] = $goods_rate['smartcontract'] + $goods_rate['realname'];
                    }else{
                        $data['risk_amount'] = $goods_rate['risk'];
                        $data['fixed_amount'] = $goods_rate['smartcontract'] + $goods_rate['realname'] + $goods_rate['risk'];
                    }
                    $data['withhold_rate'] = $goods_rate['withhold'];
                    if($pay_type == Order::ORDER_PAY_TYPE_ANTCHAIN && $source_id == 7){//店铺扫码 智能合同订单费率+0.5%
                        $data['withhold_rate'] = $goods_rate['withhold'] + 0.5;
                    }
                    if($goods_id == 1141){
                        $data['withhold_rate'] = 44.95;
                    }elseif ($goods_id == 820){
                        $data['withhold_rate'] = 41.95;
                    }elseif ($goods_id == 1024){
                        $data['withhold_rate'] = 16.95;
                    }
                    $data['shop_rate'] = $goods_rate['platform'] + $data['withhold_rate'] + $goods_rate['file'] + $data['financing_rate'] + $insure_rate + $data['huabei_rate'];
                    $data['smartcontract_amount'] = $goods_rate['smartcontract'];
                    $data['realname_amount'] = $goods_rate['realname'];
                    $data['platform_rate'] = $goods_rate['platform'];
                    $data['file_rate'] = $goods_rate['file'];
                    $data['is_preposition'] = $goods_rate['is_preposition'];
                    $data['insure_rate'] = $insure_rate;
                    $data['create_time'] = date('Y-m-d H:i:s');
                    $data['update_time'] = date('Y-m-d H:i:s');
                   return $this->where('order_id',$order_id)->update($data);
                }
            }
        }else{
            return true;
        }
    }
    //获取订单费率
    protected function getOrderRate($order_id,$is_risk){
        $data = $this->where('order_id',$order_id)->select('shop_rate','smartcontract_amount','realname_amount','risk_amount','platform_rate','withhold_rate','file_rate','financing_rate','insure_rate','huabei_rate')->first();
        $data = $data ? $data->toArray() : [];
        if(!empty($data)){
            if($is_risk == Order::RISK_SKIP_YES){//关闭风控
                $data['fixed_fee'] = $data['smartcontract_amount'] + $data['realname_amount'];
                $data['risk_amount'] = 0;
            }else{
                $data['fixed_fee'] = $data['smartcontract_amount'] + $data['realname_amount'] + $data['risk_amount'];
            }
        }
        return $data;
    }
}
